<!-- author: xuett -->
<!-- date: 2025-07-29 11:09:49 -->
<!-- description: 办理弹窗 -->
<template>
  <el-dialog v-model="visible" title="办理" width="800px" top="5vh" append-to-body>
    <div class="form-box">
      <el-form>
        <el-row>
          <el-col :span="12">
            <el-form-item label="报告编号" prop="orgName">{{ curDissentInfo.reportNo }}</el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="领域" prop="orgName">{{ curDissentInfo.field }}</el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <template v-if="curDissentInfo.field == '公安' && curDissentInfo.scope == '刑事裁判'">
        <table class="cf-table">
          <tbody>
            <tr>
              <td>犯罪记录</td>
              <td colspan="3">{{ curDissentInfo.data['犯罪记录'] }}</td>
            </tr>
            <tr>
              <td>佐证材料</td>
              <td colspan="3">{{ curDissentInfo.evidenceName }}</td>
            </tr>
          </tbody>
        </table>
      </template>
      <template v-else>
        <table class="cf-table">
          <tbody v-html="createCfContent(curDissentInfo)"></tbody>
        </table>
      </template>
    </div>

    <template #footer>
      <div class="dialog-footer" style="display: flex; justify-content: center">
        <el-button v-if="curDissentInfo.status == '0'" type="danger" icon="Close" @click="handleReject()" v-hasPermi="['credit:dissent:edit']">
          驳回
        </el-button>

        <el-button
          v-if="curDissentInfo.status == '0'"
          type="success"
          icon="CircleCheck"
          @click="handleComplete()"
          v-hasPermi="['credit:dissent:edit']"
          >完成</el-button
        >
      </div>
    </template>
  </el-dialog>
  <!-- 驳回/完成 意见 -->
  <el-dialog v-model="dialogVisible" :title="operateTitle" width="800px" top="5vh" append-to-body>
    <el-row>
      <el-input v-model="remark" :autosize="{ minRows: 2 }" type="textarea" maxlength="500" placeholder="请输入" />
    </el-row>
    <template #footer>
      <div class="dialog-footer">
        <el-button type="primary" @click="confirmSubmit">确 定</el-button>
        <el-button @click="dialogVisible = false">取 消</el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script setup name="SelectUser" lang="ts">
import { DissentVO } from '@/api/xydz/dissent/types';
import { dissentReject, dissentComplete } from '@/api/xydz/dissent/office';
import xydzCfList from '@/assets/js/xydz_cf';

const { proxy } = getCurrentInstance() as ComponentInternalInstance;
const $emit = defineEmits(['updateList']);

const state = reactive({
  curDissentInfo: {} as DissentVO,
  visible: false,
  dialogVisible: false,
  operateTitle: '',
  remark: ''
});
const { curDissentInfo, visible, dialogVisible, operateTitle, remark } = toRefs(state);

const show = (row: DissentVO) => {
  state.curDissentInfo = JSON.parse(JSON.stringify(row));

  state.curDissentInfo.data = row.data ? JSON.parse(row.data) : {};
  state.visible = true;
};

//生成处罚条目内容
let xydzCfObj = {};
xydzCfList.forEach((item) => {
  xydzCfObj[item.name] = item.propsList;
});
const createCfContent = (row) => {
  let html = '';
  let propsList = xydzCfObj[row.scope];
  propsList.forEach((item, index) => {
    html += index % 2 ? '' : '<tr>';
    html += `
        <td style="width: 150px">${item.name}</td>
        <td >${row.data[item.prop] ? row.data[item.prop] : ''}</td>`;
    html += index % 2 ? '</tr>' : '';
  });
  if (propsList.length % 2) {
    html += '</tr>';
  }
  html += `
    <tr>
      <td>佐证材料</td>
      <td colspan="3">${row.evidenceName}</td>
    </tr>`;
  return html;
};

// 驳回
const handleReject = async () => {
  state.remark = '';
  state.operateTitle = '驳回意见';
  state.dialogVisible = true;
};

// 完成
const handleComplete = async () => {
  state.remark = '';
  state.operateTitle = '完成备注';
  state.dialogVisible = true;
};

//确认驳回/完成
const confirmSubmit = () => {
  let params = { detailId: state.curDissentInfo.id, operation: state.operateTitle == '驳回意见' ? 'denied' : 'done', remark: '' };
  if (state.remark) {
    params.remark = state.remark;
  }
  let Func = state.operateTitle == '驳回意见' ? dissentReject : dissentComplete;
  Func(params).then((res) => {
    if (res.code == 200) {
      proxy?.$modal.msgSuccess('操作成功');
      state.dialogVisible = false;
      closeDialog();
    } else {
      proxy?.$modal.msgError(res.msg);
    }
  });
};

//关闭弹窗
const closeDialog = () => {
  state.visible = false;
  $emit('updateList');
};

// 暴露
defineExpose({
  show
});
</script>

<style lang="scss" scoped>
.form-box {
  width: 100%;
  padding: 20px;
}
.cf-table {
  width: 100%;
  border-collapse: collapse;
  background-color: #f6f6f6;
  :deep(td) {
    padding: 5px;
    border: 1px solid #666;
    font-size: 14px;
    &:nth-child(odd) {
      font-weight: bold;
    }
  }
}
</style>
